<?php

/**
 * Description of Article.
 *
 * @author Ian Clarence <ian.clarence@gmail.com>
 */
class Article extends ParentChild {

    /**
     * The table name.
     */
    const TABLE = 'article';

    /**
     * The media which this article contains.
     *
     * @var array
     */
    protected $media = array();

    /**
     * The HTML forms which this article contains.
     *
     * @var array
     */
    protected $htmlForms = array();

    /**
     * The array of keyphrases for this article.
     *
     * @var array
     */
    protected $keyphrases = array();

    /**
     * The constructor.
     *
     * @param array $fields
     */
    public function __construct(array $fields = array()) {
        parent::__construct($fields);
        // $this->fetchMedia();
        // $this->fetchHtmlForms();
    }

    /**
     * Fetching the array of media which are in this article.
     */
    private function fetchMedia() {
        $this->media = ArticleMedium::searchFor(self::TABLE, $this->getId(), 'medium');
    }

    /**
     * Fetching the array of HTML forms which are in this article.
     */
    private function fetchHtmlForms() {
        $this->htmlForms = ArticleHtmlForm::searchFor(self::TABLE, $this->getId(), 'html_form');
    }

    /**
     * Fetching the array of keyphrases for this article.
     */
    private function fetchKeyphrases() {
        $this->keyphrases = ArticleKeyphrase::searchFor(self::TABLE, $this->getId(), 'keyphrase');
    }

    /**
     * Getter for the array of media.
     * 
     * @return array
     */
    public function getMedia() {
        $this->fetchMedia();
        return $this->media;
    }

    /**
     * Getter for the array of HTML forms.
     * 
     * @return array
     */
    public function getHtmlForms() {
        $this->fetchHtmlForms();
        return $this->htmlForms;
    }

    /**
     * Getter for the array of keyphrases.
     *
     * @return array
     */
    public function getKeyphrases() {
        return $this->keyphrases;
    }
    
    /**
     * Getting text strings relating to the article: heading, content, etc.
     * 
     * @param string $field
     * @return string
     */
    public function getText($field) {
        $viewFields = $this->getFields();
        if (!$viewFields[$field]) {
            return '';
        }
        $language = Utils::getLanguage();
        
        $apcKey = static::TABLE . 'text_article_' . $this->getId() . '_' . $field . '_' . $language;
        $params = array(
            'field' => $field, 
            'language' => $language, 
            'text_key' => $viewFields[$field]
        );
        $callback = function($params) {
            $query = new Query('
                SELECT `tm`.`message`
                FROM `text_message` AS `tm`
                INNER JOIN `article` AS `a`
                ON `a`.:field = `tm`.`text_key`
                INNER JOIN `language` AS `l`
                ON `l`.`id` = `tm`.`language`
                WHERE `l`.`code` = :language
                AND `tm`.`text_key` = :text_key;
            ');
            $query->bindFieldName('field', $params['field']);
            $query->bindVariable('language', $params['language']);
            $query->bindVariable('text_key', $params['text_key'], FALSE);
            $result = System::getDB()->queryArray($query, TRUE);
            return $result['message'];
        };        
        return Utils::apcCache($apcKey, $callback, STORED_AS_CACHE == YES, $params);
    }

    /**
     * Getting article parameters for passing into a view.
     *
     * @return array
     */
    public function getViewFields() {
        $viewFields = $this->getFields();
        
        /**
         * Text fields. 
         */
        $viewFields['heading_text'] = $this->getText('heading');
        $viewFields['short_content_text'] = $this->getText('short_content');
        $viewFields['content_text'] = $this->getText('content');
        $viewFields['metatag_description_text'] = $this->getText('metatag_description');
        $viewFields['head_title_text'] = $this->getText('head_title');
        
        $viewFields = array_merge($viewFields, parent::getViewFields());

        /**
         * Array of media.
         */
        $this->fetchMedia();
        $viewFields['media'] = Medium::getFieldsFromRecords($this->media);

        /**
         * Array of HTML forms.
         */
        $this->fetchHtmlForms();
        $htmlForms = $this->getHtmlForms();
        $viewFields['html_forms'] = HtmlForm::getFieldsFromRecords($this->htmlForms);
        foreach ($htmlForms as $htmlFormKey => $htmlForm) {
            if ($htmlForm instanceof HtmlForm) {
                $viewFields["html_forms"][$htmlFormKey] = $htmlForm->getViewFields();
            }
        }

        /**
         * Array of keyphrases.
         */
        $this->fetchKeyphrases();
        $viewFields['keyphrases'] = TextKey::getFieldsFromRecords($this->keyphrases);
        
        return $viewFields;
    }

}